package com.kmb.business.fastdfs;


import com.kmb.commons.dto.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/upload")
public class FastDFSController {

    @Autowired
    private FastDFSClient fastDFSClient;
    @Value("${fdfs.fastdfsPreffix}")
    public String fastdfsPreffix;

    private final String THUMB_SUFFIX = "_80x80";


    @PostMapping("/uploadFile")
    public Response uploadFile(@RequestParam("files") MultipartFile multipartFile) throws IOException {
        String url = fastDFSClient.uploadFile(multipartFile);
        Map<String, String> urls = new HashMap<>();
        urls.put("fileurl", fastdfsPreffix + "/" + url);
        return Response.ok(urls);
    }

    @PostMapping("/deleteFile")
    public Response deleteFile() {
        String fileUrl = "M00/05/02/D0sSql0-3IqABdUtAAAx7h9NYJ0924_80x80.png";
        fastDFSClient.deleteFile(fileUrl);
        return Response.ok();
    }


}
